<?php
header('Content-Type: text/plain');
require_once dirname(__FILE__) . '/../../../DbUtils.class.php';
$myPdo = DbUtils::createPdoInst();

$action = $_POST['action'];

try {
  $myPdo->beginTransaction();
  switch ($action) {
    case 'submit':
      submit($myPdo);
      default:
      break;
  }
  $myPdo->commit();
} catch (PDOException $e) {
  echo ERROR_SIGN . $e->getMessage();
}

function submit($myPdo) {
  $q = json_decode($_POST['q']);
  require_once dirname(__FILE__) . '/data.inc.php';
  require_once dirname(__FILE__) . '/../../../includes/MiscUtils.class.php';
  $cond_vals = new stdClass();
  $cond_vals->c = 'payment_id = :v1';
  $cond_vals->v = array(':v1' => $q->id);
  $result = DbUtils::get(DbUtils::createPdoInst(), TABLE_NAME_PREFIX_PAYMENT . $folder . '_payment', $cond_vals, NULL, NULL, NULL, NULL, NULL);
  if (count($result->d) == 1) {
    $result->d[0]->payment_id = $q->id;
    $result->d[0]->instruction = $q->instruction;
    $result->d[0]->redirect_link = $q->redirect_link;
    DbUtils::update($myPdo, TABLE_NAME_PREFIX_PAYMENT . $folder . '_payment', $result->d[0]);
  } else {
    $tmp = new stdClass();
    $tmp->track_id = MiscUtils::unique();
    $tmp->payment_id = $q->id;
    $tmp->instruction = $q->instruction;
    $tmp->redirect_link = $q->redirect_link;
    DbUtils::add($myPdo, TABLE_NAME_PREFIX_PAYMENT . $folder . '_payment', $tmp);
  }
  
  $cond_vals = new stdClass();
  $cond_vals->c = 't.id = :v1';
  $cond_vals->v = array(':v1' => $q->id);
  $tmp = DbUtils::get($myPdo, 'pz_ecom_mthd_payment', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d[0];
  $tmp->width = $q->width;
  $tmp->height = $q->height;
  $tmp->folder = $folder;
  DbUtils::update($myPdo, 'pz_ecom_mthd_payment', $tmp);
  echo json_encode(array());
}
?>